﻿using System;
using Flagwind.Services;
using Flagwind.Resources;

namespace Flagwind.Collections.Commands
{
	[CommandOption("queues", Type = typeof(string), Description = "${Text.QueueCommand.Options.Queues}")]
	public class QueueCountCommand : CommandBase<CommandContext>
	{
		#region 构造方法

		public QueueCountCommand() : this("Count")
		{
		}

		public QueueCountCommand(string name) : base(name)
		{
		}

		#endregion

		#region 执行方法

		protected override object OnExecute(CommandContext context)
		{
			var result = 0;
			var queues = QueueCommandHelper.GetQueues(context.CommandNode, context.Expression.Options.GetValue<string>("queues"));

			foreach(var queue in queues)
			{
				var count = queue.Count;

				if(count > 0)
				{
					context.Output.WriteLine(CommandOutletColor.DarkGreen, ResourceUtility.GetString("Text.QueueCountCommand.Message", queue.Name, count));
				}
				else
				{
					context.Output.WriteLine(CommandOutletColor.DarkRed, ResourceUtility.GetString("Text.QueueIsEmpty", queue.Name));
				}
			}

			return result;
		}

		#endregion
	}
}